const router = require('@koa/router')();
const jwt = require('../utils/jwt.js')
const { findNoteListByType, findNoteDetailById, notePublish } = require('../controllers/index.js')
const { formateTime } = require('../utils/formateTime.js')

router.get('/findNoteListByType', jwt.verify(), async (ctx, next) => {
  // 检验Token合理再去数据库中查找数据返回给前端
  const { note_type } = ctx.request.query
  try {
    const res = await findNoteListByType(note_type, ctx.userId)
    if (res.length) {
      ctx.body = {
        code: '800',
        data: res,
        msg: '查询成功'
      }
    } else {
      ctx.body = {
        code: '800',
        data: res,
        msg: '当前分类下没有数据'
      }
    }
  } catch (error) {
    ctx.body = {
      code: '805',
      data: error,
      msg: '服务器异常'
    }
  }
})

router.get('/findNoteDetailById', jwt.verify(), async (ctx) => {
  const { id } = ctx.request.query

  try {
    const res = await findNoteDetailById(id)
    if (res.length) {
      ctx.body = {
        code: '800',
        data: res,
        msg: '查询成功'
      }
    } else {
      ctx.body = {
        code: '801',
        msg: '查询失败'
      }
    }
  } catch (error) {
    ctx.body = {
      code: '802',
      msg: '服务器异常',
      data: error
    }
  }
})

router.post('/note-publish', jwt.verify(), async(ctx) => {
  console.log(123123);
  const { title, note_type, note_content, head_img } = ctx.request.body
  const c_time = formateTime(new Date())
  const m_time = formateTime(new Date())

  try {
    const res = await notePublish({ 
      title, 
      note_type, 
      note_content, 
      head_img, 
      c_time, 
      m_time, 
      userId: ctx.userId, 
      nickname: ctx.nickname 
    })

    if (res.affectedRows) {
      ctx.body = {
        code: '800',
        data: 'success',
        msg: '发布成功'
      }
    } else {
      ctx.body = {
        code: '804',
        data: 'fail',
        msg: '发布失败'
      }
    }
  } catch (error) {
    ctx.body = {
      code: '805',
      data: error,
      msg: '服务端异常'
    }
  }
})

module.exports = router